package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.User;
import com.qst.pojo.Userlist;
import com.qst.service.UserlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller public class UserlistController {

	 @Autowired private UserlistService userlistService;

	 /**
	  * @description:查看个人信息清单
	  * @param:
	  * @return:
	  */
	 @RequestMapping("/userlist/get") public String findhasuserlist(HttpSession httpSession,
			 Model model) throws Exception {
		  User user1 = (User) httpSession.getAttribute("user");
		  Integer user_id = user1.getId();
		  Userlist userlist = userlistService.findhasuserlist(user_id);
		  model.addAttribute("userlist", userlist);
		  model.addAttribute("mainPage", "../common/userlistupdate.jsp");
		  if(userlist==null){
				model.addAttribute("msg", "请及时完善个人信息！");
		  }
		  return "common/main";
	 }

	 // 查找并更新用户信息
	 @RequestMapping("/userlist/update") public String updateUserlist(Model model, Userlist userlist,
			 HttpSession httpSession) throws Exception {
		  if (userlist.getId() == null) {
				String idcard = userlist.getIdcard();
				Userlist list = userlistService.checkuserlist(idcard);

				if (list != null) {
					 model.addAttribute("msg", "该身份证已被绑定,一个身份证号码只能被一个账户绑定！");
					 model.addAttribute("mainPage", "../common/userlistupdate.jsp");
					 model.addAttribute("userlist", userlist);
				} else {
					 User user1 = (User) httpSession.getAttribute("user");
					 Integer user_id = user1.getId();
					 userlist.setUser_id(user_id);
					 userlistService.insertuserlist(userlist);
					 Userlist list1 = userlistService.checkuserlist(idcard);
					 model.addAttribute("msg", "个人信息完善成功");
					 model.addAttribute("mainPage", "../common/userlistupdate.jsp");
					 model.addAttribute("userlist", list1);
					 httpSession.setAttribute("ulist", list1);
				}
		  } else {
				Userlist list = userlistService.finduserlistupdate(userlist);
				if (list != null) {
					 model.addAttribute("msg", "该身份证号码已被绑定");
					 model.addAttribute("mainPage", "../common/userlistupdate.jsp");
					 model.addAttribute("userlist", userlist);
				} else {
					 userlistService.updateuserlist(userlist);
					 model.addAttribute("msg", "更新成功");
					 model.addAttribute("mainPage", "../common/userlistupdate.jsp");
					 model.addAttribute("userlist", userlist);
					 httpSession.setAttribute("ulist", userlist);
				}
		  }
		  return "common/main";
	 }

	 @RequestMapping("/findalluserlist") public String findalluserlist(Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
			 @RequestParam(defaultValue = "zuke") String type) {
		  PageHelper.startPage(page, pageSize);
		  List<Userlist> userlist = userlistService.findalluserlist(type);
		  PageInfo<Userlist> p = new PageInfo<Userlist>(userlist);
		  model.addAttribute("userlist", userlist);
		  model.addAttribute("type", type);
		  model.addAttribute("p", p);
		  model.addAttribute("mainPage", "../userlist/userlist.jsp");
		  return "common/main";

	 }

	 // 删除用户信息
	 @RequestMapping("/deleteuserlist") public String deleteuserlist(Model model, Integer id) {
		  userlistService.deleteuserlist(id);
		  model.addAttribute("error", "deletesuccess");
		  return "redirect:findalluserlist.action";
	 }
}
